/**
 * 数组扁平化
 * @param {String} str 
 */
function transformArray(str) {

    // let reg = /[^\{{+}^\}]/ig;
    let arr = str.split(',');
    let reg = /[^\"{+}^\"]/ig
        // console.log(arr);
    let res = []; //返回结果

    for (let i = arr.length - 1; i >= 0; i--) {
        let tempArr = arr[i].match(reg);
        let index = Number(tempArr[0]);
        if (res.length === 0) {
            //初始化res
            res.length = index;
        }
        let num = ''; //数字拼接
        for (let j = 2; j < tempArr.length; j++) {
            num += tempArr[j];
        }
        res[index - 1] = Number(num);
    }

    //处理空
    for (let i = 0; i < res.length; i++) {
        if (!res[i]) {
            res[i] = 0;
        }

    }
    return res;
}

function jsonToArray(obj) {

    let result = [];
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            const element = obj[key];
            result[key - 1] = element;
            // console.log(key, element);
        }
    }
    // console.log(obj);
    console.log(result);
    //处理空
    for (let i = 0; i < result.length; i++) {
        if (!result[i]) {
            result[i] = 0;
        }

    }
    return result;
}
// console.log(`[${transformArray('{"1":123,"2":234,"8":456}')}]`);

console.log(jsonToArray(JSON.parse(JSON.stringify({ "1": 123, "2": 234, "8": 456 }))));